<?php
namespace Wap\Model;
use Think\Model;
use Think\Log;

/**
*
*/
class TeacherModel extends Model
{
	protected $tableName = "praise_item";
	protected $maxPage = 10;
	/*
	*@method get_info
	*@函数描述 获取指导教师的排名、指导作品的数量、电话号码
	*@param $praise int 活动信息
	*@param $teacher_name string 教师名字
	*@param $teacher_telephone string 教师电话
	*@param $options array
	*@return array 教师排名、指导教师数量、电话号码、教师名称
	*
	**/
	public function get_rank( $praise = -1, $teacher_name = "", $teacher_telephone = "", $options = array() )
	{
		$fields = array( 'id', 'teacher_name', 'teacher_telephone', 'sum(votes)' => 'votes', 'count(*)' => 'counts' );
		$condition['praise_id'] = $this->scopePraiseID( $praise );
		$condition['teacher_name'] = $this->scopeTeacherName( $teacher_name );
		$condition['teacher_telephone'] = $this->scopeTeacherTelephone( $teacher_telephone );
        $condition['status'] = 1;

		$page = isset( $options['page'] ) ? $options['page'] : 1;
		$limit = isset( $options['limit']) ? $options['limit'] : 5;

		$result = $this->paginate( $condition, $fields, $page, $limit );
		$data = $result['datas'][0];

        $condition = array();
        $condition['praise_id'] = $this->scopePraiseID( $praise );
        $condition['status'] = 1;
		$condition['votes'] = array( 'gt', $data['votes'] );

        $condition['teacher_telephone'] = array( "exp", "IS NOT NULL" );
        $condition['teacher_name'] = array( "exp", "IS NOT NULL" );

		//$data['rank'] = $this->field( "sum(votes) as votes, count( * ) as rank" )->where( $condition )->group( "teacher_telephone, teacher_name" )->select();

        $data['rank'] = $this->query( "select count(*) as rank from ( select sum( votes ) as votes from esm_praise_item where praise_id = $praise AND status = 1 AND teacher_telephone IS NOT NULL AND teacher_name IS NOT NULL group by teacher_name, teacher_telephone ) as tmp where votes > ".$data['votes'] );
        if( count( $data['rank'] ) ){
            $data['rank'] = $data['rank'][0]['rank'];
        }
        //$data['rank'] = 1;

		return $data;
	}

	public function ranks( $praise = -1, $page = 1, $limit = 5 )
	{
		$fields = array( 'id', 'teacher_name', 'teacher_telephone', 'sum(votes)' => 'votes', 'count(*)' => 'counts' );
		$conditions['praise_id'] = $this->scopePraiseID( $praise );
		$conditions['teacher_name'] = $this->scopeTeacherName( '' );
		$conditions['teacher_telephone'] = $this->scopeTeacherTelephone( '' );
        $conditions['status'] = 1;

		$result = $this->paginate( $conditions, $fields, $page, $limit );
		return $result;
	}

	private function scopePraiseID( $praise_id )
	{
		$condition = array( 'eq', $praise_id );
		return $condition;
	}

	private function scopeTeacherName( $teacher_name )
	{
		$condition = array( 'exp', "" );
		if( empty($teacher_name) ){
			$condition[1] = '!=""';
		}else{
			$condition[0] = 'eq';
			$condition[1] = $teacher_name;
		}
		return $condition;
	}

	private function scopeTeacherTelephone( $teacher_telephone )
	{
		$condition = array( 'exp', '' );
		if( empty($teacher_telephone) ){
			$condition[1] = '!=""';
		}else{
			$condition[0] = 'eq';
			$condition[1] = $teacher_telephone;
		}
		return $condition;
	}

	private function paginate( $condition, $fields = array(), $page = 1, $limit = 5 )//TODO: 不够定制化
	{
		$count = ceil( D($this->tableName)->where( $condition )->count() / $limit );

		if( $page > $this->maxPage ){
			$page = $count + 1;
		}

		$result['datas'] = D( $this->tableName )->field( $fields )
			->where( $condition )
			->group( "teacher_name, teacher_telephone" )
			->order( "votes desc, add_time asc" )
			->page( $page, $limit )
			->select();

		$result['curren'] = $page;
		$result['count'] = $count;
		$result['limit'] = $limit;
		$result['pre'] = $page - 1;
		$result['next'] = $page + 1;
		$result['size'] = count( $result['datas'] );

		return $result;
	}
}
